package org.misalen.web.server.controllers.machine;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.web.database.model.mysql.pro.ProDriverLocation;
import org.misalen.web.database.model.mysql.pro.ProDriverUser;
import org.misalen.web.database.model.mysql.pro.ProVideoReward;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.machine.MachineLocationForm;
import org.misalen.web.server.forms.machine.MachineRunningtimeForm;
import org.misalen.web.server.forms.machine.MachineUserInfoForm;
import org.misalen.web.server.forms.machine.MachineVideoForm;
import org.misalen.web.server.result.Empty;
import org.misalen.web.server.result.machine.MachineVideoResult;
import org.misalen.web.server.util.FormUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "视频相关接口", description = "视频列表", index = 000)
@RequestMapping(value = ServerUrl.MachineVideoControllerUrl)
public class MachineVideoController extends BaseController {

	@RequestMapping(value = "/userinfo", method = { RequestMethod.POST })
	@InterfaceApi(value = "获取用户信息(主要是用户主键 后边的接口会用到)", resultClass = { ProDriverUser.class })
	public @ResponseBody ServerResult userinfo(@RequestBody MachineUserInfoForm form) throws CustomerException {
		FormUtil.validate(form);
		ProDriverUser driverUser = proDriverUserRepository.findByMachine(form.getMachineId(), form.getType());
		if (driverUser == null) {
			return ServerResult.error(ErrorCode.SYS_ERROR_CODE_10005);
		}
		return ServerResult.ok(driverUser);
	}

	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	@InterfaceApi(value = "视频列表(不分页,用返回数据里的list就行)", resultClass = { PaginationFrom.class, MachineVideoResult.class })
	public @ResponseBody ServerResult list(@RequestBody MachineVideoForm form) throws CustomerException {
		FormUtil.validate(form);
		List<Map<String, Object>> maps = proVideoInfoRepository.findAllByDriverUserType(form.getUserId(),
				form.getType());
		return ServerResult.ok(maptoMachineVideo(maps));
	}

	@RequestMapping(value = "/location", method = { RequestMethod.POST })
	@InterfaceApi(value = "上传位置", resultClass = { Empty.class })
	public @ResponseBody ServerResult location(@RequestBody MachineLocationForm form) throws CustomerException {
		FormUtil.validate(form);
		ProDriverLocation driverLocation = new ProDriverLocation();
		driverLocation.setAddTime(new Date());
		driverLocation.setDriverUserId(form.getUserId());
		driverLocation.setLatitude(new BigDecimal(form.getLatitude()));
		driverLocation.setLongitude(new BigDecimal(form.getLongitude()));
		proDriverLocationRepository.save(driverLocation);
		return ServerResult.ok();
	}

	@RequestMapping(value = "/runningtime", method = { RequestMethod.POST })
	@InterfaceApi(value = "上传视频播放时长", resultClass = { Empty.class })
	public @ResponseBody ServerResult runningtime(@RequestBody MachineRunningtimeForm form) throws CustomerException {
		FormUtil.validate(form);
		//logger.info("上传视频播放时长(userid:" + form.getUserId() + ", videoId:" + form.getVideoId() + ")");
		ProVideoReward proVideoReward = proVideoRewardRepository.getByVideoUser(form.getUserId(), form.getVideoId());
		if (proVideoReward.getPlayedTime().longValue() < proVideoReward.getPlayeTime().longValue() && //
				form.getRunningtime().longValue() >= proVideoReward.getPlayeTime().longValue()) {
			logger.info("上传视频播放时长，更新余额，冻结金额(userid:" + form.getUserId() + ", videoId:" + form.getVideoId() + ")");
			proDriverUserRepository.updateBalance(proVideoReward.getMoney(), proVideoReward.getUserId());
			proDriverUserRepository.updateFreezeBalance(-proVideoReward.getMoney(), proVideoReward.getUserId());
		}
		proVideoRewardRepository.updatePlayedTime(form.getUserId(), form.getVideoId(), form.getRunningtime());
		return ServerResult.ok();
	}

	private PaginationFrom<MachineVideoResult> maptoMachineVideo(List<Map<String, Object>> maps) {
		List<MachineVideoResult> list = new ArrayList<MachineVideoResult>();
		for (Map<String, Object> machineVideoResult : maps) {
			MachineVideoResult result = new MachineVideoResult();
			result.setVideoId(String.valueOf(machineVideoResult.get("videoId")));
			result.setVideoUrl(String.valueOf(machineVideoResult.get("videoUrl")));
			result.setVideoThumUrl(String.valueOf(machineVideoResult.get("videoThumUrl")));
			list.add(result);
		}
		PaginationFrom<MachineVideoResult> paginationFrom = new PaginationFrom<MachineVideoResult>();
		paginationFrom.setList(list);
		paginationFrom.setPage(1);
		paginationFrom.setRows(list.size());
		paginationFrom.setTotal((long) list.size());
		return paginationFrom;
	}
}
